﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using EveAI.Product;
using EveAI.Live;


namespace FeedReader
{
    public partial class Form1 : Form
    {

        public class Destroyed
        {
            public string typeName { get; set; }
            public long typeID { get; set; }
            public string itemSlot { get; set; }
            public long qtyDropped { get; set; }
            public long qtyDestroyed { get; set; }
        }

        public class Dropped
        {
            public string typeName { get; set; }
            public long typeID { get; set; }
            public string itemSlot { get; set; }
            public long qtyDropped { get; set; }
            public long qtyDestroyed { get; set; }
        }

        public class Items
        {
            public List<Destroyed> destroyed { get; set; }
            public List<Dropped> dropped { get; set; }
        }

        public class RootObject
        {
            public long internalID { get; set; }
            public long externalID { get; set; }
            public string victimExternalID { get; set; }
            public string victimName { get; set; }
            public string victimShipName { get; set; }
            public long ISK { get; set; }
            public Items items { get; set; }
        }

        private void GrabShips()
        {
            List<string> shipStr = new List<string>();
            TreeNode mainNode = new TreeNode();
            mainNode.Name = "Ships";
            mainNode.Text = "Ships";
            treeViewShips.Nodes.Add(mainNode);

            EveAI.DataCore dataCore = new EveAI.DataCore();

            EveAI.DataReader dr = new EveAI.StaticDataDataReader(dataCore);
            dr.Read(EveAI.DataTypes.Names | EveAI.DataTypes.ProductAttributes | EveAI.DataTypes.Production | EveAI.DataTypes.Products | EveAI.DataTypes.NpcData | EveAI.DataTypes.BasicMapData);
            foreach (ProductType product in dataCore.ProductTypes)
            {
                if (product.Group.Category.Name.Equals("Ship"))
                {
                    shipStr.Add(product.Name);

                }
            }

            shipStr.Sort();
            foreach (string ship in shipStr)
            {
                mainNode.Nodes.Add(ship);
            }

        }

        public Form1()
        {
            InitializeComponent();
            
            this.Cursor = Cursors.WaitCursor;
            
            try
            {
                GrabShips();
               
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.ToString();
            }

            this.Cursor = Cursors.Default;
        }

        private void treeViewShips_AfterSelect(object sender, TreeViewEventArgs e)
        {

            this.Cursor = Cursors.WaitCursor;

            try
            {
                textBox1.Clear();
                string reqString = "http://eve-kill.net/epic/victimShip:" + treeViewShips.SelectedNode.Text + "/mailLimit:" + textBoxNumKM.Text + "/onlyapi:true/mask:1311036";
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(reqString);
                myRequest.Method = "GET";
                myRequest.Timeout = 6000;
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();

                //parse json text
                List<RootObject> listOfCategories = new List<RootObject>();

                JavaScriptSerializer jss = new JavaScriptSerializer();

                //Deserialize

                // deserialize as a list of Categories, and put into listOfCategories
                listOfCategories = jss.Deserialize<List<RootObject>>(result);

                
                
                foreach (RootObject item in listOfCategories)
                {
                    if (item.victimName.Length > 0)
                    {
                        textBox1.AppendText(FormatEFT(item) + "\n");
                    }
                    else
                    {
                        textBox1.AppendText("Problem connecting to eve kill" + "\n");
                    }
                    
                    /*
                    textBox1.AppendText(item.victimName + ", " + item.internalID + ", " + item.externalID + ", " + item.victimShipName + ", " + item.ISK);

                    foreach (Destroyed destroyedItem in item.items.destroyed)
                    {
                        textBox1.AppendText(": " + destroyedItem.typeName.ToString());
                    }

                    textBox1.AppendText("\n");
                    foreach (Dropped droppedItem in item.items.dropped)
                    {
                        textBox1.AppendText("; " + droppedItem.typeName.ToString());
                    }


                    textBox1.AppendText("\n");
                    textBox1.AppendText("\n");
                     */
                }
                 
                sr.Close();
                myResponse.Close();


            }
            catch (Exception ex)
            {
                textBox1.Text = ex.ToString();
            }

            this.Cursor = Cursors.Default;

        }

        private string FormatEFT(RootObject Killmail)
        {
            string result = "";  // total string to return that is formatted like an eft clipboard fit

            string header = "[" + Killmail.victimShipName + ", " + Killmail.victimName + " - " + Killmail.victimExternalID + " ]" + System.Environment.NewLine;

            string lowSlots = "";
            string midSlots = "";
            string highSlots = "";
            string cargohold = "";
            string dronebay = "";
            string rigs = "";

            foreach (Destroyed destroyedItem in Killmail.items.destroyed)
            {
                    
                switch (int.Parse(destroyedItem.itemSlot))
                {
                    
                    
                    // case 1: high slot
                    case 1:
                        for (long i = 0; i < destroyedItem.qtyDestroyed; i++)
                        {
                            highSlots += destroyedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 2:
                        for (long i = 0; i < destroyedItem.qtyDestroyed; i++)
                        {
                            midSlots += destroyedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 3:
                        for (long i = 0; i < destroyedItem.qtyDestroyed; i++)
                        {
                            lowSlots += destroyedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 4:
                        for (long i = 0; i < destroyedItem.qtyDestroyed; i++)
                        {
                            cargohold += destroyedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 5:
                        for (long i = 0; i < destroyedItem.qtyDestroyed; i++)
                        {
                            rigs += destroyedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 6:
                        for (long i = 0; i < destroyedItem.qtyDestroyed; i++)
                        {
                            dronebay += destroyedItem.typeName + " x1" + System.Environment.NewLine;
                        }
                        break;
                   
                }
            }

            foreach (Dropped droppedItem in Killmail.items.dropped)
            {
                //textBox1.AppendText("; " + droppedItem.typeName.ToString());
                switch (int.Parse(droppedItem.itemSlot))
                {
                    // case 1: high slot
                    case 1:
                        for (long i = 0; i < droppedItem.qtyDropped; i++)
                        {
                            highSlots += droppedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 2:
                        for (long i = 0; i < droppedItem.qtyDropped; i++)
                        {
                            midSlots += droppedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 3:
                        for (long i = 0; i < droppedItem.qtyDropped; i++)
                        {
                            lowSlots += droppedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 4:
                        for (long i = 0; i < droppedItem.qtyDropped; i++)
                        {
                            cargohold += droppedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 5:
                        for (long i = 0; i < droppedItem.qtyDropped; i++)
                        {
                            rigs += droppedItem.typeName + System.Environment.NewLine;
                        }
                        break;
                    case 6:
                        for (long i = 0; i < droppedItem.qtyDropped; i++)
                        {
                            dronebay += droppedItem.typeName + " x1" + System.Environment.NewLine;
                        }
                        break;

                }
            }


            // concat all strings to look like eft clipboard fit
            result = header + System.Environment.NewLine + lowSlots + System.Environment.NewLine +
                        midSlots + System.Environment.NewLine + highSlots + System.Environment.NewLine + 
                        rigs + System.Environment.NewLine + dronebay;

            // add some empty space between km
            result += System.Environment.NewLine;
            result += System.Environment.NewLine;
            

            return result;

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void buttonBrowsePilot_Click(object sender, EventArgs e)
        {
            webBrowserPilot.Navigate("http://eve-kill.net/?a=pilot_detail&plt_ext_id=" + textBoxPilotID.Text);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
